package com.megvii.corelib.net;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyLog;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * Created by linyue on 15/12/27.
 */
public class MegviiMultipartRequest<T> extends MegviiJsonRequest<T>
{
    HttpEntity entity;

    public MegviiMultipartRequest(int method, String url, Response.Listener<T> listener,
            Response.ErrorListener errorListener, Class<T> clazz)
    {
        super(method, url, listener, errorListener, clazz);
    }

    @Override
    public String getBodyContentType()
    {
        return entity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError
    {
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        Map<String, byte[]> byteArrayParams = getByteArrayParams();
        if (byteArrayParams != null)
        {
            for (Map.Entry<String, byte[]> entry : byteArrayParams.entrySet())
            {
                String key = entry.getKey();
                byte[] value = entry.getValue();
                ByteArrayBody bab = new ByteArrayBody(value, "pic.png");
                entityBuilder.addPart(key, bab);
            }
        }
        Map<String, String> stringParams = getStringParams();
        if (stringParams != null)
        {
            for (Map.Entry<String, String> entry : stringParams.entrySet())
            {
                String key = entry.getKey();
                String value = entry.getValue();
//                StringBody sb = new StringBody(value, ContentType.MULTIPART_FORM_DATA);
                StringBody sb = null;
                try {
                    sb = new StringBody(value, "text", Charset.forName("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                entityBuilder.addPart(key, sb);
            }
        }
        Map<String, String[]> stringArrayParams = getStringArrayParams();
        if (stringArrayParams != null)
        {
            for (Map.Entry<String, String[]> entry : stringArrayParams.entrySet())
            {
                String key = entry.getKey();
                String[] values = entry.getValue();
                for (String value : values)
                {
                    StringBody sb = new StringBody(value, ContentType.MULTIPART_FORM_DATA);
                    entityBuilder.addPart(key, sb);
                }
            }
        }
        //TODO process getFileParams
        //TODO process getStreamParams
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try
        {
            entity = entityBuilder.build();
            entity.writeTo(bos);
        }
        catch (IOException e)
        {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        byte[] result = bos.toByteArray();
        if (bos != null)
        {
            try
            {
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }

    protected Map<String, String> getStringParams()
    {
        return null;
    }

    protected Map<String, byte[]> getByteArrayParams()
    {
        return null;
    }

    protected Map<String, String[]> getStringArrayParams()
    {
        return null;
    }
    //TODO getFileParams
    //TODO getStreamParams
}
